<?php
declare (strict_types = 1);

namespace app\home\controller;

use app\base\BaseController;
use think\facade\Db;
use think\facade\View;

class Board extends BaseController
{

    //工资社保汇总
    public function sl()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $user = Db::name('User')->where('status', '>=', 0)
                ->where('eid',$param['eid'])
                ->order('create_time asc')
                ->select()->toArray();
            $month = date('n');
            $tabVal = $param['tab'] == 1 ? 'wages' : ($param['tab'] == 2 ? 'social' : 'accumulation_fund');
            foreach ($user as $k => $v){
                $m13_sum = 0.00;
                for ($i = 1;$i <= 12; $i++){
                    $wages = Db::name('Wages')->where('status', '>=', 0)
                        ->where('uid',$v['id'])
                        ->where('year',$param['date'])
                        ->where('month',$i)
                        ->value($tabVal);
                    $user[$k]['m'.$i] = empty($wages) ? 0 : $wages;
                    $m13_sum += $user[$k]['m'.$i];
                }
                $user[$k]['m13'] = round($m13_sum,2);
            }
            $user_total = array(
                "name" => "合计 (元)",
                "educational" => "-",
                "major" => "-",
                "position" => "-",
            );
            for ($i = 1; $i <= 13; $i++) {
                $m13_sum = 0.00;
                foreach ($user as $k => $v) {
                    $m13_sum += $v['m'.$i];
                }
                $user_total['m'.$i] = $m13_sum == 0 ? 0 :round($m13_sum, 2);
            }
            $user[] = $user_total;
            $list['data'] = $user;
            return table_assign(0, '', $list);
        } else {
            if($this->eid > 0){
                $enterprise = Db::name('Enterprise')->where('id',$this->eid)->where('status', '>=', 0)->order('create_time asc')->select();
            }else{
                $enterprise = Db::name('Enterprise')->where('status', '>=', 0)->order('create_time asc')->select();
            }
            $year = date('Y');

            View::assign('enterprise', $enterprise);
            View::assign('eid', $this->eid);
            View::assign('year', $year);
            return view();
        }
    }

    //项目工资汇总
    public function pro()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $item = Db::name('Item')->where('status', '>=', 0)
                ->where('eid',$param['eid'])
                ->order('create_time asc')
                ->select()->toArray();
            $month = date('n');
            $tabVal = $param['tab'] == 1 ? 'wages' : ($param['tab'] == 2 ? 'social' : 'accumulation_fund');
            foreach ($item as $k => $v){
                $m13_sum = 0.00;
                for ($i = 1;$i <= 12; $i++){
                    //所有项目当月总工时
                    $allItemHours = Db::name('UserWorkHours')->where('status', '>=', 0)
                        ->where('eid',$param['eid'])
                        ->where('year',$param['date'])
                        ->where('month',$i)
                        ->sum('work_hours');

                    //当前项目当月总工时
                    $thisItemHours = Db::name('UserWorkHours')->where('status', '>=', 0)
                        ->where('eid',$param['eid'])
                        ->where('item_id',$v['id'])
                        ->where('year',$param['date'])
                        ->where('month',$i)
                        ->sum('work_hours');

                    //当月工资/社保/公积金
                    $thisWages = Db::name('Wages')->where('status', '>=', 0)
                        ->where('eid',$param['eid'])
                        ->where('year',$param['date'])
                        ->where('month',$i)
                        ->sum($tabVal);

                    //当前项目当月总工时 / 所有项目当月总工时 * 当月工资/社保/公积金
                    if($allItemHours == 0 || $thisItemHours == 0 || $thisWages == 0){
                        $item[$k]['m'.$i] = 0;
                    }else{
                        $item[$k]['m'.$i] = round($thisItemHours / $allItemHours * $thisWages, 2);
                    }
                    $m13_sum += $item[$k]['m'.$i];
                }
                $item[$k]['m13'] = round($m13_sum,2);
            }
            $user_total = array(
                "name" => "合计 (元)",
            );
            for ($i = 1; $i <= 13; $i++) {
                $m13_sum = 0.00;
                foreach ($item as $k => $v) {
                    $m13_sum += $v['m'.$i];
                }
                $user_total['m'.$i] = $m13_sum == 0 ? 0 :round($m13_sum, 2);
            }
            $item[] = $user_total;
            $list['data'] = $item;
            return table_assign(0, '', $list);
        } else {
            if($this->eid > 0){
                $enterprise = Db::name('Enterprise')->where('id',$this->eid)->where('status', '>=', 0)->order('create_time asc')->select();
            }else{
                $enterprise = Db::name('Enterprise')->where('status', '>=', 0)->order('create_time asc')->select();
            }
            $year = date('Y');

            View::assign('enterprise', $enterprise);
            View::assign('eid', $this->eid);
            View::assign('year', $year);
            return view();
        }
    }

    //项目材料费用汇总
    public function mat()
    {
        if (request()->isAjax()) {
            $param = get_params();

            $item = Db::name('Item')->where('status', '>=', 0)
                ->where('eid',$param['eid'])
                ->order('create_time asc')
                ->select()->toArray();
            $month = date('n');
            $data = [];
            foreach ($item as $k => $v){
                $m13_sum = 0.00;
                $data[$k]['name'] = $v['code'];
                $data[$k]['pro_name'] = $v['name'];
                $data[$k]['leader'] = Db::name('User')->where('id', $v['director_uid'])->value('name');
                for ($i = 1;$i <= 12; $i++){
                    $totalM = Db::name('GoodsCollar')->where('eid', $this->eid)->where('item_id', $v['id'])->where('out_year',$param['year'])->where('out_month',$i)->sum('money');
                    $data[$k]['m'.$i] = round($totalM,2);
                    $m13_sum += $data[$k]['m'.$i];
                }
                $data[$k]['m13'] = round($m13_sum,2);
            }
            $user_total = array(
                "name" => "合计 (元)",
                "pro_name" => "-",
                "leader" => "-",
            );

            for ($i = 1; $i <= 13; $i++) {
                $m13_sum = 0.00;
                foreach ($data as $k => $v) {
                    $m13_sum += $v['m'.$i];
                }
                $user_total['m'.$i] = $m13_sum == 0 ? 0 :round($m13_sum, 2);
            }
            $data[] = $user_total;
            $list['data'] = $data;
            return table_assign(0, '', $list);
        } else {
            if($this->eid > 0){
                $enterprise = Db::name('Enterprise')->where('id',$this->eid)->where('status', '>=', 0)->order('create_time asc')->select();
            }else{
                $enterprise = Db::name('Enterprise')->where('status', '>=', 0)->order('create_time asc')->select();
            }
            $year = date('Y');

            View::assign('enterprise', $enterprise);
            View::assign('eid', $this->eid);
            View::assign('year', $year);
            return view();
        }
    }

    //研发折旧汇总
    public function dep()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $eid = !empty($param['eid']) ? $param['eid']: $this->eid;
            $item = Db::name('Item')->where('status', '>=', 0)
                ->where('eid',$eid)
                ->order('create_time asc')
                ->select()->toArray();
            $year = !empty($param['yaer']) ? $param['yaer']: date('Y');


            $data = [];
            foreach ($item as $k => $v){
                $m13_sum = 0.00;
                $start_time = date('Y-m-d' , $v['start_time'] );
                $end_time = date('Y-m-d' , $v['start_time'] );
                $data[$k]['name'] = $v['code'];
                $data[$k]['pro_name'] = $v['name'] . "（{$start_time}至{$end_time}）";
                $data[$k]['leader'] = Db::name('User')->where('id', $v['director_uid'])->value('name');
                for ($i = 1;$i <= 12; $i++){

                    //查询在本月并且在本项目有过工时的固定资产
                    $eq_value = Db::name('EquipmentWorkHours')->alias('ewh')
                        ->join('Equipment e','e.id = ewh.eq_id','left')
                        ->where(['ewh.year'=>$year , 'ewh.month' => $i , 'ewh.item_id'=>$v['id'],'ewh.eid'=>$eid])
                        ->field('ewh.item_id,ewh.all_hours,ewh.work_hours,ewh.eq_id,e.original_value,e.loss_rate,e.period')
                        ->select()->toArray();

                    $totalM = 0;



                    if(!empty($eq_value)){
                        //循环所有固定资产
                        foreach($eq_value as $key => $value){
                            //当前设备在所有项目的合计的总工作工时
                            $sum_all_work_hours = Db::name('EquipmentWorkHours')
                                ->where(['eid' => $eid , 'year'=>$year , 'month' => $i , 'eq_id' => $value['eq_id']])
                                ->sum('work_hours');
                            $hours = 0;
                            if($sum_all_work_hours > 0){
                                $all_hours = $value['all_hours'];
                                //当前设备在当前项目的工作工时
                                $work_hours = Db::name('EquipmentWorkHours')
                                    ->where(['eid' => $eid , 'eq_id' => $value['eq_id']  , 'year'=>$year , 'month' => $i , 'item_id'=>$value['item_id']])
                                    ->sum('work_hours');
                                //研发占比
                                if($all_hours > 0){
                                    $eq_rate = round($work_hours/$all_hours , 2);
                                }else{
                                    $eq_rate = 0;
                                }
                                //计算当前固定资产的月折旧额
                                $m_loss_rate = round($value['original_value'] * (1-($value['loss_rate']/100))/$value['period']/12 , 2);

                                //研发折旧
                                $yfzj_value = round($eq_rate * $m_loss_rate , 2);

                                if($yfzj_value > 0 ){
                                    $hours = round($work_hours / $sum_all_work_hours * $yfzj_value , 2);
                                }
                            }

                            $totalM += $hours;
                        }
                    }

                    $data[$k]['m'.$i] = $totalM;
                    $m13_sum += $data[$k]['m'.$i];

                }
                $data[$k]['m13'] = round($m13_sum,2);
            }

            $user_total = array(
                "name" => "合计 (元)",
                "pro_name" => "-",
                "leader" => "-",
            );

            for ($i = 1; $i <= 13; $i++) {
                $m13_sum = 0.00;
                foreach ($data as $k => $v) {
                    $m13_sum += $v['m'.$i];
                }
                $user_total['m'.$i] = $m13_sum == 0 ? 0 :round($m13_sum, 2);
            }
            $data[] = $user_total;
            $list['data'] = $data;
            return table_assign(0, '', $list);
        } else {

            $year = date('Y');
            View::assign('year', $year);
            return view();
        }

    }



}
